package com.nifostasky.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nifostasky.comic.entitys.SysDict;
import com.nifostasky.comic.entitys.SysDictDomain;
import com.nifostasky.comic.exceptions.BusinessException;
import com.nifostasky.comic.mapper.SysDictDomainMapper;
import com.nifostasky.comic.service.SysDictDomainService;
import com.nifostasky.comic.service.SysDictService;
import com.nifostasky.comic.utils.PageUtils;
import com.nifostasky.comic.vos.ResultCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class SysDictDomainServiceImpl extends ServiceImpl<SysDictDomainMapper, SysDictDomain>
        implements SysDictDomainService {

    @Autowired
    private SysDictService sysDictService;

    @Override
    public IPage<SysDictDomain> findListPage(PageUtils pageUtils, SysDictDomain dictDomain) {
        LambdaQueryWrapper<SysDictDomain> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dictDomain.getDomainName()), SysDictDomain::getDomainName, dictDomain.getDomainName());
        qw.eq(StringUtils.isNotBlank(dictDomain.getDomainCode()), SysDictDomain::getDomainCode, dictDomain.getDomainCode());
        IPage<SysDictDomain> page = this.baseMapper.selectPage(new Page<>(pageUtils.getPageIndex(), pageUtils.getPageSize()), qw);
        return page;
    }

    @Override
    public List<SysDictDomain> findList(SysDictDomain dictDomain) {
        LambdaQueryWrapper<SysDictDomain> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dictDomain.getDomainName()), SysDictDomain::getDomainName, dictDomain.getDomainName());
        qw.eq(StringUtils.isNotBlank(dictDomain.getDomainCode()), SysDictDomain::getDomainCode, dictDomain.getDomainCode());
        return this.list(qw);
    }

    @Override
    public Boolean saveOrUpdateData(SysDictDomain dictDomain) {
        //验证编码是否重复
        SysDictDomain sysDictDomain = this.getDictDomainByCode(dictDomain.getDomainCode());
        if (Objects.nonNull(sysDictDomain)) {
            if (Objects.isNull(dictDomain.getId())) {
                throw new BusinessException(ResultCode.DICT_DOMAIN_CODE_EXIST);
            } else if (Objects.nonNull(dictDomain.getId()) && !Objects.equals(dictDomain.getId(), sysDictDomain.getId())) {
                throw new BusinessException(ResultCode.DICT_DOMAIN_CODE_EXIST);
            }
        }
        return this.saveOrUpdate(dictDomain);
    }

    @Override
    public SysDictDomain getDictDomainByCode(String domainCode) {
        LambdaQueryWrapper<SysDictDomain> qw = new LambdaQueryWrapper<>();
        qw.eq(SysDictDomain::getDomainCode,domainCode);
        return this.getOne(qw);
    }

    @Override
    public Boolean deleteByIds(String ids) {
        if(StringUtils.isBlank(ids)){
            throw new BusinessException(ResultCode.REQUIRED_PARAMS);
        }
        List<String> listId = Arrays.asList(ids.split(","));
        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysDict::getDomainId,listId);
        List<SysDict> listDict = sysDictService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(listDict)){
            throw new BusinessException("包含字典小类不允许删除");
        }
        return this.removeByIds(listId);
    }
}
